//
//  Poolinfo_ViewController.m
//  duoduo
//
//  Created by wanghao on 16/10/11.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "Poolinfo_ViewController.h"
#import "LXSigmentScrollView.h"
#import "Pool_commentVC.h"
#import "ScrollView.h"
#import "Pool_infoVC.h"
#import "PayOrderVC.h"

@interface Poolinfo_ViewController ()<seletedControllerDelegate,UIScrollViewDelegate>
{
    ScrollView *titleScroll;
    UIScrollView *mainScroll;
}
@property (nonatomic, strong) LXSigmentScrollView * SingmentScrollView;
@property (nonatomic, strong) Pool_infoVC * infoVC;
@property (nonatomic, strong) Pool_commentVC * commentVC;
@end

@implementation Poolinfo_ViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];

    [self initUI];

}


//购买
- (void)buyClick
{

}

//初始化两个控制器,详情和评论
- (void)initUI
{
    //创建头部视图(滑动视图)
    titleScroll= [[ScrollView alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, 40)];
    NSArray *mu = @[@"详情",@"评论"];

    titleScroll.headArray = mu.mutableCopy;

    //设置代理
    titleScroll.SeletedDelegate = self;
    //添加
    self.navigationItem.titleView = titleScroll;
    //[self.view addSubview:titleScroll];

    //创建一个滑动视图用来滑动Viewcontroller
    mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-60)];
    //设置代理
    mainScroll.delegate = self;
    //设置滑动区域
    mainScroll.contentSize = CGSizeMake(ScreenWidth*mu.count, 0) ;
    //打开分页功能
    mainScroll.pagingEnabled = YES;
    //设置背景颜色
    mainScroll.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mainScroll];
    //设置当前子控制器

    self.infoVC = [[Pool_infoVC alloc] init];
    self.commentVC = [[Pool_commentVC alloc] init];

    [self addChildViewController:_infoVC];
    _infoVC.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    //将当前的子视图控制器的view添加到主滑动视图上
    [mainScroll addSubview:_infoVC.view];

    [self addChildViewController:_commentVC];
    _commentVC.view.frame = CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight);
    [mainScroll addSubview:_commentVC.view];

}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (mainScroll.contentOffset.x < ScreenWidth) {
         self.commentVC = [[Pool_commentVC alloc] init];
        [self addChildViewController:self.commentVC];
        self.commentVC.view.frame = CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight);
        [mainScroll addSubview:self.commentVC.view];
    }


    [titleScroll changeBtntitleColorWith:scrollView.contentOffset.x/ScreenWidth+1000];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [titleScroll changeBtntitleColorWith:scrollView.contentOffset.x/ScreenWidth+1000];

}

#pragma mark -头部scrollView的代理方法的实现
-(void)seletedControllerWith:(UIButton *)btn{

    mainScroll.contentOffset = CGPointMake(ScreenWidth*(btn.tag - 1000), 0);

    if (mainScroll.contentOffset.x < ScreenWidth) {
        self.commentVC = [[Pool_commentVC alloc] init];
        [self addChildViewController:self.commentVC];
        self.commentVC.view.frame = CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight);
        [mainScroll addSubview:self.commentVC.view];
    }
    [titleScroll changeBtntitleColorWith:mainScroll.contentOffset.x/ScreenWidth+1000];


}

@end
